package com.icow.wms.basic.controller;

import java.util.List;

import com.icow.wms.basic.domain.Door;
import com.icow.wms.basic.utils.DoorHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.DoorPeripheral;
import com.icow.wms.basic.service.IDoorPeripheralService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

/**
 * doorController
 *
 * @author bcs
 * @date 2024-08-13
 */
@RestController
@RequestMapping(value = {"/peripheral/door", "/peripheral"})
public class DoorPeripheralController extends BaseController {
    @Autowired
    private IDoorPeripheralService doorPeripheralService;

    /**
     * 查询door列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DoorPeripheral doorPeripheral) {
        startPage();
        List<DoorPeripheral> list = doorPeripheralService.selectDoorPeripheralList(doorPeripheral);
        return getDataTable(list);
    }

    /**
     * 导出door列表
     */
    @Log(title = "door", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DoorPeripheral doorPeripheral) {
        List<DoorPeripheral> list = doorPeripheralService.selectDoorPeripheralList(doorPeripheral);
        ExcelUtil<DoorPeripheral> util = new ExcelUtil<DoorPeripheral>(DoorPeripheral.class);
        return util.exportExcel(list, "peripheral");
    }

    /**
     * 获取door详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(doorPeripheralService.selectDoorPeripheralById(id));
    }

    @GetMapping(value = "/openDoor/{id}")
    public AjaxResult openDoor(@PathVariable("id") Long id) {
        DoorPeripheral doorPeripheral = doorPeripheralService.selectDoorPeripheralById(id);
        if (doorPeripheral == null) {
            return AjaxResult.error(-1, "未找到对应门设备");
        }
        Door door = DoorHandler.IP_DOOR_MAP.get(doorPeripheral.getIp());
        if (door == null) {
            return AjaxResult.error(-1, doorPeripheral.getIp() + "门不在线");
        }
        boolean openDoor = door.openDoor(doorPeripheral.getIp(), doorPeripheral.getOpenDoor());
        if (!openDoor) {
            return AjaxResult.error(-1, "开门失败,门不在线");
        }
        return AjaxResult.success(1);
    }

    @GetMapping(value = "/closeDoor/{id}")
    public AjaxResult closeDoor(@PathVariable("id") Long id) {
        DoorPeripheral doorPeripheral = doorPeripheralService.selectDoorPeripheralById(id);
        if (doorPeripheral == null) {
            return AjaxResult.error(-1, "未找到对应门设备");
        }
        Door door = DoorHandler.IP_DOOR_MAP.get(doorPeripheral.getIp());
        if (door == null) {
            return AjaxResult.error(-1, doorPeripheral.getIp() + "门不在线");
        }
        boolean openDoor = door.closeDoor(doorPeripheral.getIp(), doorPeripheral.getOpenDoor());
        if (!openDoor) {
            return AjaxResult.error(-1, "关门失败,门不在线");
        }
        return AjaxResult.success(1);
    }

    /**
     * 新增door
     */
    @Log(title = "door", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoorPeripheral doorPeripheral) {
        return toAjax(doorPeripheralService.insertDoorPeripheral(doorPeripheral));
    }

    /**
     * 修改door
     */
    @Log(title = "door", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoorPeripheral doorPeripheral) {
        return toAjax(doorPeripheralService.updateDoorPeripheral(doorPeripheral));
    }

    /**
     * 删除door
     */
    @Log(title = "door", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(doorPeripheralService.deleteDoorPeripheralByIds(ids));
    }


}